<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>部门管理</title>
    <link rel="stylesheet" href="/layui/css/layui.css">
</head>
<body>
<!--列表显示-->
<table id="deptTable" lay-filter="deptFilter"></table>

<script type="text/html" id="barRowEdit">
    <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>

<!--工具栏模板：-->
<script type="text/html" id="toolbarHead">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-sm" lay-event="add">添加</button>
        <button class="layui-btn layui-btn-sm" lay-event="delete">删除</button>
        <button class="layui-btn layui-btn-sm" lay-event="update">编辑</button>
    </div>
</script>

<form id="deptAddForm" class="layui-form" style="display:none"> <!-- 提示：如果你不想用form，你可以换成div等任何一个普通元素 -->
    <div class="layui-form-item">
        <label class="layui-form-label">部门名称</label>
        <div class="layui-input-block">
            <input type="text" name="deptName"  aria-required="true"  placeholder="请输入" required lay-verify="required" autocomplete="off" class="layui-input">
        </div>
    </div><div class="layui-form-item">
        <label class="layui-form-label">排序</label>
        <div class="layui-input-block">
            <input type="text" name="orderNum" placeholder="请输入" required lay-verify="required" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">部门状态</label>
        <div class="layui-input-block">
            <input type="radio" name="status" value="0" title="启动" checked>
            <input type="radio" name="status" value="1" title="禁用">
        </div>
    </div>

    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn" lay-submit lay-filter="deptAddFilter">立即提交</button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>
    </div>
    <!-- 更多表单结构排版请移步文档左侧【页面元素-表单】一项阅览 -->
</form>


<form id="deptUpdateForm" class="layui-form" lay-filter="deptUpdateFilter" style="display:none"> <!-- 提示：如果你不想用form，你可以换成div等任何一个普通元素 -->
    <input type="hidden" name="deptId">
    <div class="layui-form-item">
        <label class="layui-form-label">部门名称</label>
        <div class="layui-input-block">
            <input type="text" name="deptName" placeholder="请输入" required lay-verify="required" autocomplete="off" class="layui-input">
        </div>
    </div><div class="layui-form-item">
        <label class="layui-form-label">排序</label>
        <div class="layui-input-block">
            <input type="text" name="orderNum" placeholder="请输入" required lay-verify="required" autocomplete="off" class="layui-input">
        </div>
    </div>



    <div class="layui-form-item">
        <label class="layui-form-label">部门状态</label>
        <div class="layui-input-block">
            <input type="radio" name="status" value="0" title="启动" checked>
            <input type="radio" name="status" value="1" title="禁用">
        </div>
    </div>

    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn" lay-submit lay-filter="deptUpdateFilter">立即提交</button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>
    </div>
</form>

<script src="/layui/layui.js"></script>
<script>
    layui.use(['layer','table','jquery','form'], function(){
        var table = layui.table;
        var layer = layui.layer;
        var $ = layui.jquery;
        var form = layui.form;

        //第一个实例
        var deptTable = table.render({
            elem: '#deptTable'
            ,toolbar: '#toolbarHead'
            ,url: '/dept/queryAll/' //数据接口
            ,page: true //开启分页
            ,cellMinWidth:80
            ,cols: [
                [ //表头
                 {type: 'checkbox',fixed: 'left'}
                ,{field: 'deptId', title: 'ID',  sort: true, fixed: 'left'}
                ,{field: 'deptName', title: '部门名称', sort:true}
                ,{field: 'orderNum', title: '排序',  sort: true}
                ,{field: 'status', title: '部门状态',
                    templet: function(d){
                        return d.status==0?"启用":"禁用"
                    },sort:true}
                ,{field: 'createBy', title: '创建人'}
                ,{field: 'createTime', title: '创建时间', sort: true}
                ,{field: 'updateBy', title: '更新人',  sort: true}
                ,{field: 'updateTime', title: '更新时间'}
                ,{fixed: 'right',  align:'center', toolbar: '#barRowEdit'}
            ]
            ]
        });

        //监听工具条
        table.on('tool(deptFilter)', function(obj){
            var data = obj.data;
            if(obj.event === 'del'){
                layer.confirm('真的删除行么', function(index){
                    $.ajax({
                        url:"/dept/deleteById",
                        type:"DELETE",
                        data:{deptId:data.deptId},
                        dataType:"json",
                        success:function(resp){
                            if(resp.code==0){
                                layer.msg(resp.msg);
                                deptTable.reload();
                            }
                        },
                        error:function(e){
                            layer.msg("请求失败");
                        }
                    });
                    obj.del();
                    layer.close(index);
                });
            } else if(obj.event === 'edit'){
                // layer.alert('编辑行：<br>'+ JSON.stringify(data))
                form.val("deptUpdateFilter",data);
                layer.open({
                    type: 1,
                    area:['500px','500px'],
                    content: $('#deptUpdateForm') //这里content是一个DOM，注意：最好该元素要存放在body最外层，否则可能被其它的相对元素所影响
                });
            }
        });

        //头部工具栏监听事件
        table.on('toolbar(deptFilter)', function(obj){
            var checkStatus = table.checkStatus(obj.config.id);
            var checkData = checkStatus.data;
            switch(obj.event){
                case 'add':
                    // layer.msg('添加');
                    // $("#deptAddForm").reset();
                    document.getElementById("deptAddForm").reset();
                    layer.open({
                        type: 1,
                        area:['500px','500px'],
                        content: $('#deptAddForm') //这里content是一个DOM，注意：最好该元素要存放在body最外层，否则可能被其它的相对元素所影响
                    });
                    break;
                case 'delete':
                    // layer.msg('删除');
                    // layer.msg(JSON.stringify(checkData));
                    if(checkData.length==0){
                        layer.msg('至少选择一行');
                        return;
                    }
                    layer.confirm('真的删除吗',function(index){
                        var deptIds = "";
                        for(var i = 0; i < checkData.length; i++){
                            deptIds+=checkData[i].deptId+",";
                        }
                        deptIds=deptIds.substr(0,deptIds.length-1);
                        layer.msg(deptIds);
                        $.ajax({
                            url:"/dept/deleteByIds",
                            type:"DELETE",
                            data:{deptIds:deptIds},
                            dataType:"json",
                            success:function(resp){
                                if(resp.code==0){
                                    layer.msg(resp.msg);
                                    deptTable.reload();
                                }
                            },
                            error:function(e){
                                layer.msg("请求失败");
                            }
                        });
                    });
                    break;
                case 'update':
                    // layer.msg('编辑');
                    if(checkData.length!=1){
                        layer.msg('只能选中一行');
                        return;
                    }
                    form.val("deptUpdateFilter",checkData[0]);
                    layer.open({
                        type: 1,
                        area:['500px','500px'],
                        content: $('#deptUpdateForm') //这里content是一个DOM，注意：最好该元素要存放在body最外层，否则可能被其它的相对元素所影响
                    });
                    break;
            };
        });

        //监听提交
        form.on('submit(deptAddFilter)', function(data){
            layer.msg(JSON.stringify(data.field));
            $.ajax({
                url:"/dept/insert",
                type:"POST",
                data:data.field,
                dataType:"json",
                success:function(resp){
                    if(resp.code==0){
                        layer.msg(resp.msg);
                        layer.closeAll('page');
                        deptTable.reload();
                    }
                },
                error:function(e){
                    layer.msg("请求失败");
                }
            });
            return false;
        });

        form.on('submit(deptUpdateFilter)', function(data){
            layer.msg(JSON.stringify(data.field));
            $.ajax({
                url:"/dept/update",
                type:"PUT",
                data:data.field,
                dataType:"json",
                success:function(resp){
                    if(resp.code==0){
                        layer.msg(resp.msg);
                        layer.closeAll('page');
                        deptTable.reload();
                    }
                },
                error:function(e){
                    layer.msg("请求失败");
                }
            });
            return false;
        });
    });
</script>
</body>
</html>